<?php 
// +----------------------------------------------------------------------
// | 狂团[kt8.cn]旗下KtAdmin是为独立版SAAS系统而生的快速开发框架.
// +----------------------------------------------------------------------
// | [KtAdmin] Copyright (c) 2022 http://ktadmin.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\base\model\admin\app;
use think\facade\Db;
use think\facade\Session;
use app\base\model\BaseModel;
use think\facade\Cache;

/*
* 应用管理
*/
class AppManageModel extends BaseModel
{
	/**
	 * 获取 主应用列表
	 * @return 
	 */
	public static function mainapp($req)
	{
		$uid = Session::get('uid');
		$data = [];
		$fileList = getFileList('mainfest.xml');
		$res = [];
		foreach ($fileList as $file) {
			$xml = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
			$xmlArr = json_decode(json_encode($xml), 1);
			if(isset($xmlArr['application']) && $xmlArr['application']['type'] == 1 && $xmlArr['application']['adminindex']){
				$info = Db::table('kt_base_market_app')->where(['code'=>$xmlArr['application']['code']])->find();
				$xmlArr['application']['name'] = $info['name']?:$xmlArr['application']['name'];
				$xmlArr['application']['logo'] = $info['logo']?:$xmlArr['application']['logo'];
				$xmlArr['application']['description'] = $info['describe']?:$xmlArr['application']['description'];
			 	$xmlArr['application']['adminindex'] = $req->domain().$xmlArr['application']['adminindex'];
			 	$res[] = $xmlArr['application'];
			} 

		}
		// $agent = Db::table('kt_base_agent')->find($uid);
		// if($agent['admin'] == 1){

		// }
		return $res;
	}

	/**
	 * 获取 子应用（主应用插件）
	 * @return 
	 */
	public static function plugin($req)
	{
		$uid = Session::get('uid');
		$data = [];
		$fileList = getFileList('mainfest.xml');
		$res = [];
		foreach ($fileList as $file) {
			$xml = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
			$xmlArr = json_decode(json_encode($xml), 1);
			if(isset($xmlArr['application']) && $xmlArr['application']['type'] == 2 && $xmlArr['application']['adminindex']){
				$info = Db::table('kt_base_market_app')->where(['code'=>$xmlArr['application']['code']])->find();
				$xmlArr['application']['name'] = $info['name']?:$xmlArr['application']['name'];
				$xmlArr['application']['logo'] = $info['logo']?:$xmlArr['application']['logo'];
				$xmlArr['application']['description'] = $info['describe']?:$xmlArr['application']['description'];
			 	$xmlArr['application']['adminindex'] = $req->domain().$xmlArr['application']['adminindex'];
			 	$res[] = $xmlArr['application'];
			} 
		}
		return $res;
	}

	/**
	 * 获取 工具应用
	 * @return 
	 */
	public static function tools($req)
	{
		$uid = Session::get('uid');
		$data = [];
		$fileList = getFileList('mainfest.xml');
		$res = [];
		foreach ($fileList as $file) {
			$xml = simplexml_load_file($file,'SimpleXMLElement', LIBXML_NOCDATA);
			$xmlArr = json_decode(json_encode($xml), 1);
			if(isset($xmlArr['application']) && $xmlArr['application']['type'] == 3 && $xmlArr['application']['adminindex']){ 
				$info = Db::table('kt_base_market_app')->where(['code'=>$xmlArr['application']['code']])->find();
				$xmlArr['application']['name'] = $info['name']?:$xmlArr['application']['name'];
				$xmlArr['application']['logo'] = $info['logo']?:$xmlArr['application']['logo'];
				$xmlArr['application']['description'] = $info['describe']?:$xmlArr['application']['description'];
				$xmlArr['application']['adminindex'] = $req->domain().$xmlArr['application']['adminindex'];
				$res[] = $xmlArr['application'];
			};
		}
		return $res;
	}
}